<template>
  <div>
    <el-form
      class="query-form"
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      size="small"
      label-width=""
    >
      <el-form-item class="big-query" label="项目" prop="appid">
        <el-select v-model="queryParams.appid"
          filterable
          clearable
          placeholder="请选择"
          @change="handleQuery">
          <el-option v-for="item in permission_projectList" :value="item.app_id" :label="item.name" :key="item.app_id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <div>
      <el-button type="primary"
        icon="el-icon-plus"
        style="margin-bottom:10px"
        size="small"
        @click="handleAdd">新增</el-button>
    </div>
    <el-table v-loading="loading"
      :data="dataList">
      <el-table-column label="序号"
        type="index"
        width="50">
      </el-table-column>
      <el-table-column label="使用上限">
        <template slot-scope="scope">
          {{ scope.row.amount }}{{ scope.row.unit_id|translateToName(chargeUnitList,'unit_id','name') }} / {{ scope.row.frequency === 'month'? '每月':'每天' }}
        </template>
      </el-table-column>
      <el-table-column label="所属项目">
        <template slot-scope="scope">
          <span>{{ scope.row.appid|translateToName(permission_projectList,'app_id','name') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述"
        min-width="120"
        prop="describe">
      </el-table-column>
      <el-table-column label="操作"
        width="120"
        fixed="right">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            @click="handleUpdate(scope.row)">修改</el-button>

          <el-button
            size="small"
            type="text"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.page_size"
      @pagination="getList" />

    <el-dialog :title="dialogTitle"
      :visible.sync="open"
      width="650px"
      :close-on-click-modal="false"
      @closed="onDialogClosed">
      <el-form ref="form"
        :model="form"
        :rules="rules"
        label-width="100px">
        <el-form-item label="项目" prop="appid">
          <el-select v-model="form.appid"
            filterable
            :disabled="form.id"
            placeholder="请选择">
            <el-option v-for="item in permission_projectList" :value="item.app_id" :label="item.name" :key="item.app_id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="消耗上限" required>
          <el-row>
            <el-col :span="7">
              <el-form-item prop="amount">
                <el-input-number v-model="form.amount" :min="1" :precision="0" controls-position="right" style="width: 140px;"></el-input-number>
              </el-form-item>
              </el-col>
            <el-col :span="9">
              <el-form-item prop="unit_id">
                <el-select v-model="form.unit_id" style="width: 180px;" :disabled="form.id">
                  <el-option v-for="item in chargeUnitList" :value="item.unit_id" :label="item.internal_name" :key="item.unit_id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label=""
                prop="frequency">
                <el-radio-group v-model="form.frequency">
                  <el-radio :key="1"
                    :label="'month'">每月</el-radio>
                  <el-radio :key="2"
                    :label="'day'">每天</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="描述" prop="describe">
          <el-input v-model.trim="form.describe" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary"
          :loading="saveLoading"
          @click="submitForm(false)">确 认</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import { getUseLimitList, addUseLimit, updateUseLimit, deleteUseLimit } from '@/api/addition'
import { getChargeUnit } from '@/api/system'
export default {
  name: 'AdditionUseLimit',
  data () {
    return {
      loading: false,
      saveLoading: false,
      chargeUnitList: [],
      fnList: [],
      queryParams: {
        appid: null,
        page: 1,
        page_size: 10
      },
      dataList: [],
      total: 0,
      open: false,
      form: {
        id: null,
        appid: null,
        amount: null,
        unit_id: null,
        describe: null,
        frequency: 'day'
      },
      rules: {
        appid: [{ required: true, message: '项目不能为空', trigger: 'change' }],
        amount: [{ required: true, message: '不能为空', trigger: 'blur' }],
        unit_id: [{ required: true, message: '不能为空', trigger: 'change' }]
      }
    }
  },
  computed: {
    ...mapGetters('zcsd-core', ['permission_projectList']),
    dialogTitle () {
      return this.form.id ? '修改' : '新增'
    }
  },
  created () {
    getChargeUnit().then(res => {
      this.chargeUnitList = res.data.list
    })
    this.getList()
  },
  methods: {
    // 点击搜索
    handleQuery () {
      this.queryParams.page = 1
      this.getList()
    },
    handleAdd () {
      this.open = true
    },
    getList () {
      this.loading = true
      // const params = Object.assign({}, this.queryParams)
      // if (params.appid?.length) {
      //   params.appid = params.appid.join(',')
      // } else {
      //   delete params.appid
      // }

      getUseLimitList(this.queryParams).then(res => {
        const data = res.data || {}
        this.dataList = data.list
        this.total = data.count
      }).finally(() => {
        this.loading = false
      })
    },
    handleUpdate (row) {
      this.open = true

      this.$nextTick(() => {
        Object.keys(this.form).forEach(key => {
          this.form[key] = row[key]
        })
      })
    },
    submitForm () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.saveLoading = true
          const fn = this.form.id ? updateUseLimit : addUseLimit
          fn(this.form).then(() => {
            this.getList()
            this.msgSuccess(this.form.id ? '修改成功' : '添加成功')

            this.open = false
          }).finally(() => {
            this.saveLoading = false
          })
        }
      })
    },
    onDialogClosed () {
      this.resetForm('form')
      this.form.id = null
    },
    /** 删除按钮操作 */
    handleDelete ({ id, appid }) {
      const { page, page_size } = this.queryParams
      const name = this.$options.filters.translateToName(appid, this.permission_projectList, 'app_id', 'name')
      this.$confirm('是否确认删除"' + name + '"的消耗上限?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUseLimit({ id }).then(() => {
          this.queryParams.page = this.getCurrentPage({ total: this.total, pageSize: page_size, pageIndex: page })
          this.getList()
          this.msgSuccess('删除成功')
        })
      })
    }
  }
}
</script>
